package gui.startUpWindow;

import gui.InitDBinfoWindow;
import gui.mainWindow.MainWindow;
import gui.newTournamentWindow.NewTournamentWindow;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import core.Constants;
import dataBase.DbHolder;

public class StartupWindowController {

	/**
	 * shows the InitDBinfoWindow until the DB is initialized
	 */
	protected static void initDB() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				InitDBinfoWindow.getInstance().setLocationRelativeTo(null);
				InitDBinfoWindow.getInstance().setVisible(true);
			}
		});
		DbHolder.getInstance().getArcherData(); // to start the DB
		InitDBinfoWindow.getInstance().dispose();
	}

	/**
	 * @return true if file is chosen, false otherwise
	 */
	protected boolean openTournament() {
		final JFileChooser chooser = new JFileChooser();
		final FileFilter filter = new FileNameExtensionFilter("xml Dateien", Constants.XML_FILEENDING);
		chooser.addChoosableFileFilter(filter);
		final int ret = chooser.showDialog(null, "Datei öffnen");

		if (ret == JFileChooser.APPROVE_OPTION) {
			final File fileToOpen = chooser.getSelectedFile();

			/** open the main Window */
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					MainWindow inst;
					inst = new MainWindow(fileToOpen);
					inst.setLocationRelativeTo(null);
					inst.setVisible(true);
				}
			});
			return true;
		} else {
			return false;
		}
	}

	protected void createNewTournament() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				NewTournamentWindow.getInstance().setLocationRelativeTo(null);
				NewTournamentWindow.getInstance().setVisible(true);
			}
		});
	}
}
